﻿import fl.controls.DataGrid;
import fl.data.DataProvider;

var sampleItem:Object;
var dg:DataGrid;
var dp:DataProvider; 


	input.text = "Thi5 is a tricky-66 example.";
	 term.text = "\\b([\\w]+)([-]?)([\\'\\w]*)\\b";
	 
	 b1.addEventListener(MouseEvent.CLICK,buttonClick);
     function buttonClick(e:MouseEvent) {
		var pattern1:RegExp = new RegExp(term.text, "g");
		trace("test");
		var wordSplit:Array = input.text.match(pattern1);
		output.text= wordSplit.toString(', ');
		countWords(wordSplit);
       }
	   
	//var allWords = {"words":[], "counts"[]};   

	function countWords(inputArray) {
		var allWords:Object = new Object();
		allWords.words = [];
		allWords.count = {};
	
		for (var i in inputArray) {
			if (allWords.count[inputArray[i]]) {
				allWords.count[inputArray[i]] += 1;
			} else {
				allWords.words.push(inputArray[i]);
				allWords.count[inputArray[i]] = 1;
			}
		}
		dg=new DataGrid;
		dp = new DataProvider();
		dg.columns=["Word","Count"];
		dg.dataProvider = dp;
		dg.setSize(249.95, 256.15);
		dg.move(281.95, 134.5);

		output2.text= "";
		for (i in allWords.words) {
			//output2.text += allWords.words[i] + " - " + allWords.count[allWords.words[i]] + " ";
			dp.addItem({Word:allWords.words[i],Count:allWords.count[allWords.words[i]]});
		}
			dp.sortOn("Count", Array.NUMERIC | Array.DESCENDING);
			addChild(dg);
		
	}
	
